# -*- coding: iso-8859-2 -*-

import re
import string
import os

from authorshipanalyzer.authorslot import AuthorFiller
from authorshipanalyzer.textutils import Splitter
from authorshipanalyzer.clp import ClpUtils
from authorshipanalyzer.file import ResourceReader
from authorshipanalyzer.file import Path

class WordFeaturesFiller(AuthorFiller):
    '''
    Fill slot with word based features
    '''
    
    def __init__(self):
        self.splitter = Splitter()
        self.clpUtils = ClpUtils()
        self.functionWords = ResourceReader().readWordList(Path.getResourceDir() + os.sep + "function_words.txt")
        
    
    def fill(self, authorslot):
        '''
        Fill a slot
        '''
        sumOfWordLengths = 0.0
        hapaxLegomena = 0.0
        shortWordNum = 0.0
        functionWordsNum = 0.0
        
        posCounter = 0.0
        N = 0.0
        V = 0.0
        
        for slot in authorslot.slots:     
                  
            V += len(slot.wordFrequency) 
            
            # POS
            posList = self.splitter.getPosList(slot.text, True)
            for pos in posList:
                if pos != "unknown":
                    posCounter += 1.0
                    if pos in authorslot.wPos.keys():
                        authorslot.wPos[pos] += 1.0
                    else:
                        authorslot.wPos[pos] = 1.0
            
            for word, freq in slot.wordFrequency.items():
                N += freq
                
                if authorslot.wordFrequency.has_key(word):
                    authorslot.wordFrequency[word] += freq
                else:
                    authorslot.wordFrequency[word] = freq
                
                wordLen = len(word)
                sumOfWordLengths += wordLen
                
                if wordLen <= 3.0:
                    shortWordNum += 1.0
                
                if freq == 1.0:
                    hapaxLegomena += 1.0
                                      
                # check if word is a function word
                if word in self.functionWords:
                    functionWordsNum += freq
                 
        if posCounter > 0.0:
            for pos in authorslot.wPos.keys():
                authorslot.wPos[pos] /= posCounter
                
        if N > 0.0:
            authorslot.wAvgWordLength = sumOfWordLengths / N
            authorslot.wVocabularyRichness = V / N
            authorslot.wFunctionWord = functionWordsNum / N
            authorslot.wShortWords = shortWordNum / N
            authorslot.wHapaxLegomentaN = hapaxLegomena / N
        
        if V > 0.0:
            authorslot.wHapaxLegomentaV = hapaxLegomena / V
                
if __name__ == "__main__":
    from authorshipanalyzer.slot import Slot
    from authorshipanalyzer.message import Message
    from authorshipanalyzer.analyze import FooterAnalyzer
    
    m = Message()
    m.setAuthor("asd")
    m.setText("kiedy wreszcie bedzie wojna wojna")

    slot = Slot()
    slot.message = m
    
    f = WordFeaturesFiller()
    f.fill(slot)

    print slot.wAvgWordLength
    print slot.wVocabularyRichness
#    print slot.wFunctionWord
    print slot.wShortWords
    print slot.wHapaxLegomentaN
    print slot.wHapaxLegomentaV
    
    for pos, proc in slot.wPos.items():
        print "Part-of-Speech: " + pos + " " + str(proc) + "%"

        
            
        